package com.buaa.leecode;

import com.buaa.leecode.model.TreeNode;

import java.util.LinkedList;

/**
 * 112. Path Sum
 */
public class N112 {
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        if (root.val == sum && root.right == null && root.left == null) {
            return true;
        }
        boolean flag = false;
        if (root.left != null) {
            flag = hasPathSum(root.left, sum - root.val);
        }
        if (flag) {
            return true;
        }
        if (root.right != null) {
            flag = hasPathSum(root.right, sum - root.val);
        }
        if (flag) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        N112 solution = new N112();
        Util util = new Util();
        String input = "1,2";
        TreeNode root = util.buildInput(input);
        int fitLength = util.getMaxItemLength(input);
        System.out.println("自适应元素打印宽度：" + fitLength);
        util.print(root, fitLength);
        System.out.println(solution.hasPathSum(root, 3));
        System.out.println(solution.hasPathSum(root, 14));
    }

}
